﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.门店出入库.Service;
using Seagull.BarTender.Print;
using DXApplicationTangche.Utils;
using DXApplicationTangche.DTO;
using System.IO;

namespace DXApplicationTangche.UC.标签打印
{
    public partial class Form入库 : DevExpress.XtraEditors.XtraForm
    {
        private List<TagPrintDTO> tagPrintDTOs = new List<TagPrintDTO>();
        private TagPrintDTO tagPrint = new TagPrintDTO();
        public Form入库()
        {
            InitializeComponent();
        }

        private void textBox水洗_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (!String.IsNullOrWhiteSpace(this.textBox水洗.Text))
                {
                    try
                    {
                        //  入库
                        this.tagPrint = 出入库Service.garmentWipFactoryStockIn(this.textBox水洗.Text.Trim());
                        try
                        {
                            if (!String.IsNullOrEmpty(this.tagPrint.styleSizeCd))
                            {
                                this.tagPrint.styleSizeCd = this.tagPrint.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", "");
                            }
                        }
                        catch { }
                        //  打印入库贴
                        //this.print质检合格证();
                        //  gridcontrol显示
                        if (!String.IsNullOrEmpty(this.tagPrint.inventoryNo))
                        {
                            if (this.tagPrintDTOs.Count > 0)
                            {
                                if (!this.tagPrintDTOs.Exists(dto => dto.inventoryNo.Equals(this.tagPrint.inventoryNo)))
                                {
                                    this.tagPrintDTOs.Add(this.tagPrint);
                                }
                            }
                            else
                            {
                                this.tagPrintDTOs.Add(this.tagPrint);
                            }
                        }
                        this.gridControl入库一览.DataSource = this.tagPrintDTOs;
                        this.gridView入库一览.RefreshData();
                        this.gridView入库一览.BestFitColumns();
                        this.textBox水洗.Clear();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                else
                {
                    MessageBox.Show("请扫码");
                }
            }
        }

        private void Form入库_Load(object sender, EventArgs e)
        {
            this.ActiveControl = this.textBox水洗;
        }


        private void print质检合格证()
        {
            //获取不同模板
            String filePath质检模板 = "stock_qcpass.btw";
            String fileName质检模板 = Path.GetFileName(filePath质检模板);
            //根据相对路径判断模板是否存在
            //不存在则下载进行打印
            if (!System.IO.File.Exists(@"tag\" + fileName质检模板))
            {
                FileUtil.DownloadPicture(URL.图片url + URL.打印模板路径 + filePath质检模板, @"tag\" + fileName质检模板, -1);
            }
            String str = System.AppDomain.CurrentDomain.BaseDirectory;
            //String str = System.AppDomain.CurrentDomain.BaseDirectory;
            //新建BarTender并启动，模板位置
            Engine btEngine = new Engine();
            btEngine.Start();
            LabelFormatDocument btFormat = btEngine.Documents.Open(str + @"tag\" + fileName质检模板);
            //指定打印机名 
            btFormat.PrintSetup.PrinterName = "TSC TTP-244 Pro";
            //btFormat.PrintSetup.PrinterName = "POSTEK G-3106";
            //打印份数                   
            btFormat.PrintSetup.IdenticalCopiesOfLabel = 1;
            //改变标签打印数份连载 
            btFormat.PrintSetup.NumberOfSerializedLabels = 1;
            //对BTW模版相应字段进行赋值 

            btFormat.SubStrings["styleNameCn"].Value = this.tagPrint.styleNameCn;
            btFormat.SubStrings["materialCode"].Value = this.tagPrint.materialCode;
            btFormat.SubStrings["styleSizeCd"].Value = this.tagPrint.styleSizeCd;
            btFormat.SubStrings["inventoryNo"].Value = this.tagPrint.inventoryNo;
            btFormat.SubStrings["printDate"].Value = DateTime.Now.ToString("yyyy/MM/dd");

            Messages messages;
            int waitout = 10000; // 10秒 超时 
            Seagull.BarTender.Print.Result nResult1 = btFormat.Print("质检合格证" + this.tagPrint.inventoryNo, waitout, out messages);
            btFormat.PrintSetup.Cache.FlushInterval = CacheFlushInterval.PerSession;
            //不保存对打开模板的修改 
            btFormat.Close(SaveOptions.DoNotSaveChanges);
            //结束打印引擎                  
            btEngine.Stop();
        }

        private void simpleButton打印模板更新_Click(object sender, EventArgs e)
        {
            string saveDirectoryPath = @"tag\";
            //获取不同模板
            String filePath入库模板 = "stock_qcpass.btw";
            String fileName入库模板 = Path.GetFileName(filePath入库模板);
            //根据相对路径判断模板是否存在
            //存在则删除
            if (System.IO.File.Exists(saveDirectoryPath + fileName入库模板))
            {
                File.Delete(saveDirectoryPath + filePath入库模板);
            }
        }
    }
}